package com.example.day7;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day7.adapter.BiAdapter;
import com.example.day7.bean.BijiBean;
import com.example.day7.sql.Sql;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private Toolbar mHomeBar;
    private RecyclerView mHomeRe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initViews();
    }

    private void initViews() {
        mHomeBar = findViewById(R.id.home_bar);
        mHomeRe = findViewById(R.id.home_re);
        mHomeBar.setTitle("首页");
        setSupportActionBar(mHomeBar);
        ArrayList<BijiBean> bijiBeans = new ArrayList<>();
        Sql sql = new Sql(this);
        ArrayList<BijiBean> bijiBeans1 = sql.querySQList();
        if (bijiBeans.isEmpty()){
            for (int i = 0; i < 20; i++) {
                bijiBeans.add(new BijiBean(i,"asdf"+i,"dsfasdf"+i));
                sql.inserUser("asdf"+i,"dsfasdf"+i);
            }
        }else{
            bijiBeans.addAll(bijiBeans1);
        }
        mHomeRe.setLayoutManager(new LinearLayoutManager(this));
        mHomeRe.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        BiAdapter biAdapter = new BiAdapter(this, bijiBeans);
        mHomeRe.setAdapter(biAdapter);
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
        getMenuInflater().inflate(R.menu.menu2,menu);
        return super.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.add:
                startActivity(new Intent(HomeActivity.this,InActivity.class));
                break;
            case R.id.search:
                startActivity(new Intent(HomeActivity.this,SearchActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
